package cn.org.seventh.tools.plugin;

import org.generama.FreeMarkerTemplateEngine;
import org.generama.JellyTemplateEngine;
import org.generama.QDoxCapableMetadataProvider;
import org.generama.WriterMapper;
import org.generama.defaults.QDoxPlugin;

import com.thoughtworks.qdox.model.JavaClass;

public class SpringBeanPlugin extends QDoxPlugin {

	/**
	 * 要去掉的后缀
	 */
	private String trim;

	private String filename;

	public SpringBeanPlugin(JellyTemplateEngine templateEngine,
			QDoxCapableMetadataProvider metadataProvider,
			WriterMapper writerMapper) throws ClassNotFoundException {
		super(new FreeMarkerTemplateEngine(), metadataProvider, writerMapper);
		setMultioutput(false);
		new TagLibrary(metadataProvider);
	}

	public SpringBeanPlugin(FreeMarkerTemplateEngine templateEngine,
			QDoxCapableMetadataProvider metadataProvider,
			WriterMapper writerMapper) throws ClassNotFoundException {
		super(new FreeMarkerTemplateEngine(), metadataProvider, writerMapper);
		setMultioutput(false);
		new TagLibrary(metadataProvider);
	}

	public void setTrim(String str) {
		trim = str;
	}

	public String getName(Object metadata) {
		return getNamedParameter(metadata, "name", null);
	}

	public String getId(Object metadata) {
		return getNamedParameter(metadata, "id", null);
	}

	public String getScope(Object metadata) {
		return getNamedParameter(metadata, "scope", "singleton");
	}

	public String getAutowire(Object metadata) {
		return getNamedParameter(metadata, "autowire", "byName");
	}

	public String getDepends(Object metadata) {
		String str = getNamedParameter(metadata, "depends", null);
		if (null != str && !"".equals(str.trim())) {
			return "depends-on=\"" + str + "\"";
		}
		return "";
	}

	public String getLazy(Object metadata) {
		return getNamedParameter(metadata, "lazy", "true");
	}

	public String getCheck(Object metadata) {
		return getNamedParameter(metadata, "check", "default");
	}

	public String getIdentification(Object metadata) {
		JavaClass javaClass = (JavaClass) metadata;
		StringBuffer sb = new StringBuffer("");
		if (null != getId(metadata) && !"".equals(getId(metadata).trim())) {
			sb.append(" id=\"").append(trim(getId(metadata))).append("\"");
		}
		if (null != getName(metadata) && !"".equals(getName(metadata).trim())) {
			sb.append(" name=\"").append(trim(getName(metadata))).append("\"");
		}
		if ((null == getId(metadata) || "".equals(getId(metadata).trim()))
				&& (null == getName(metadata) || "".equals(getName(metadata)
						.trim()))) {
			String name = javaClass.getName();
			name = javaClass.getName().substring(0, 1).toLowerCase()
					+ javaClass.getName().substring(1,
							javaClass.getName().length());
			sb.append(" name=\"").append(trim(name)).append("\"");
		}
		return sb.toString();
	}

	public String getClass(Object metadata) {
		JavaClass javaClass = (JavaClass) metadata;
		return javaClass.getFullyQualifiedName();
	}

	private String trim(String str) {
		if (null != trim) {
			str = str.trim();
			if (str.endsWith(trim)) {
				str = str.substring(0, str.length() - trim.length());
			}
			return str;
		} else {
			return str.trim();
		}
	}

	private String getNamedParameter(Object metadata, String name, String value) {
		JavaClass javaClass = (JavaClass) metadata;
		String str = javaClass.getNamedParameter(SpringBeanTagImpl.NAME, name);
		return null == str ? value : str;
	}

	public boolean shouldGenerate(Object metadata) {
		JavaClass javaClass = (JavaClass) metadata;
		return javaClass.getTagByName(SpringBeanTagImpl.NAME) == null ? false
				: true;
	}

	public String getDestinationFilename(Object metadata) {
		return filename;
	}

	public String getDestinationPackage(Object metadata) {
		return ".";
	}

	public String getFilename() {
		return this.filename;
	}

	public void setFilename(String filename) {
		this.filename = filename;
	}

	public String getTrim() {
		return this.trim;
	}
}